<?php

require 'global.php';

function _index(){
	extract(user::init());
	$threadid=kc_get('threadid',2,1);
	$db=new db;
	$thread=$db->getRows_one('%s_thread','title,isbest,typeid','threadid='.$threadid);
	if (empty($thread)) {
		kc_tip('您要访问的帖子已被删除！');
	}
	$types=$GLOBALS['bar_types'];
	
	$tit=htmlspecialchars($thread['title']);

	$pid=kc_get('pid',2,1);
	$sql='username,ip,date,floor,content,userid,kid';
	$url='bar_thread_'.$threadid.'_PID.htm';
	$where='threadid='.$threadid;
	$order='floor asc,threadid asc';
	$res=$db->select('%s_reply', $sql, $url, $where, $order,'',10);
	$title=$tit.' 第'.$pid.'页 - '.kc_config('site.name');

	$s='<h1>'.$tit.'</h1>';
	$nav.='<div class="conter_nav"><a href="/">首页</a> > <a href="./">'.kc_config('site.name').'</a>';

	$nav.=' > <a href="bar_type_'.$thread['typeid'].'_1.htm">'.$types[$thread['typeid']].'</a>';
	$nav.=' > <a href="bar_thread_'.$threadid.'_1.htm">'.$tit.'</a>';
	$nav.=' > <strong>第'.$pid.'页</strong>';

	$fs.='<ul id="typeList">';
	foreach ($types as $key => $type) {
		$fs.='<li><a href="bar_type_'.$key.'_1.htm">'.$type.'</a></li>';
	}

	$fs.='</ul>';
	
	$user=user::check();
	




	if (!empty($res)) {

		foreach ($res as $rs) {
			$s.='<table width="100%" cellspacing="1"><tr><th width="15%"><strong>'.$rs['username'].'</strong></th>';
			$s.='<td width="85%"class="top"><i>发表于：'.str::formatDate($rs['date'],'Y-m-d h:i:s',1).'</i>';
			if($user['userid']==$rs['userid'] || $user['gid']=='1'){
			$s.='<a href="/browse/kuidb/index.php?action=edt&threadid='.$threadid.'&rid='.$rs['floor'].'">编辑</a>';
			}
			$s.='<em><a href="#" title="回到顶部" id="'.$rs['floor'].'">#'.$rs['floor'].'楼#</a></em></td></tr><tr valign="top"><th>';
			
			$s.='<img src="'.kc_userherd($rs['userid']).'"/>';
			$s.='</th><td align="left" valign="top" ><div class="co_bs">'.$rs['content'].kid($rs['kid'],$rs['floor']).'</div></td></tr></table>';
		}
	}
	$s.=' </div>';
	$s.='<div class="page_page k_pagelist">'.$db->pagelist.'</div>';
	
	$s.='<script type="text/javascript">';
	$s.='$(pageInit);';
	$s.="function pageInit(){\$('#content').xheditor(true,{tools:'GStart,Pastetext,GEnd,Separator,GStart,Fontface,Bold,Italic,Underline,FontColor,BackColor,Removeformat,GEnd,Separator,GStart,Align,List,Outdent,Indent,GEnd,Separator,GStart,Link,Unlink,Img,Flash,Media,Emot,GEnd,Separator,GStart,About,GEnd',upImgUrl:'upload.php',upImgExt:'jpg,jpeg,gif,png'})}";
	$s.='</script>';

	$s.='<form id="post">';
	$s.='<ul>';
	
	$s.='<li>';
	$s.='<strong class="h50">发表回复</strong>';
	$s.='<textarea name="content" id="content" cols="90" rows="3"></textarea>';
	$s.='</li>';

	$s.='</ul>';
	$s.='<div class="k_submit">';
	$s.='<input id="but" type="hidden" value="add" name="but">';
	//$s.='<input type="submit" class="huifu" value="回复!" onclick="$.kc_ajax({URL:\'thread.php\',CMD:\'post\',FORM:\'post\',METHOD:\'POST\',threadid:'.$threadid.'});return false;" />';
	$s.='<input type="submit" class="huifu" value="回复!" onclick="$.kc_ajax({URL:\'/browse/kuidb/index.php\',CMD:\'bbsedt\',FORM:\'post\',\'threadid\':'.$threadid.'});return false;" />';	
	
	
	$s.='<strong style="margin-left:10px;">请输入验证码：</strong>';
	$s.='<input type="text" maxlength="4" class="k_verify k_in w50" name="k_verify" id="k_verify"/><input type="hidden" name="k_verify_salt" id="k_verify_salt" />';
	$s.='<em class="k_verify" id="verify_image"></em>';
	$s.='<input type="hidden" id="verify_salt" name="verify_salt"/>';
	$s.='<span style="color:#F00;margin-left:10px;">共同营造和谐环境，杜绝诬陷、谩骂及骗子信息！</span>';
	$s.='</div>';

	$s.='</form>';



	$db->update('%s_thread',array('hit'=>'[[hit+1]]'),'threadid='.$threadid);
	
	require ROOT.'template/bbs.html';
}


function _post(){
	extract(user::init());
	user::lockip();
	$threadid=kc_post('threadid',2,1);
	if ($userid==0) kc_ajax(array('JS'=>'alert(\'请先登录或注册成为会员后再发布帖子!\')'));;
	if(empty($_POST['content'])) kc_ajax(array('JS'=>'alert(\'内容不能为空!\')'));
	if(str::len($_POST['content'])>50000) kc_ajax(array('JS'=>'alert(\'内容过长，不能超过50000个字符!\')'));
	user::blackword($_POST['content']);
	//验证码
	$salt=$_POST['verify_salt'];
	if(empty($_POST['verify'])) kc_ajax(array('JS'=>'alert(\'请输入验证码!\')'));
	$fullCode=file::get(PATH_CACHE.'/verify/'.$salt.'.php');
	$code=substr($fullCode,13);
	$js="alert('您输入的验证码有误!');\$.kc_ajax({URL:'verify.php',id:'verify',salt:'$salt'});";
	if($code!=strtoupper($_POST['verify'])) kc_ajax(array('JS'=>$js));
	file::delete(PATH_CACHE.'/verify/'.$salt.'.php');
	
	$array=array(
		'lastuserid'=>$userid,
		'lastusername'=>$username,
		'lastdate'=>$time,
		'reply'=>'[[reply+1]]',
	);
	$db=new db;
	$db->update('%s_thread', $array,'threadid='.$threadid);
	
	
	
	
		$kuicontent=kc_post('content');
	
								$kuicontent=preg_replace('/<a ([^>]*)>|<\/a>/is','',$kuicontent);
				
								$kuicontent=preg_replace('/<(table|tbody|thead|tr|td|th|caption|script|iframe) ?([^>]*)>|<\/(table|tbody|thead|tr|td|th|caption|script|iframe)>/is','',$kuicontent);
					
			
			

	
	
	
	
	
	
	$array=array(
		'threadid'=>$threadid,
		'userid'=>$userid,
		'username'=>$username,
		'ip'=>str::ip(),
		'date'=>$time,
		'floor'=>$floor=$db->newid('%s_reply','threadid='.$threadid,'floor'),
		'content'=>$kuicontent
	);
	$db->insert('%s_reply',$array);

	$pid=ceil($floor/10);
	
	kc_ajax(array('JS'=>"go('bar_thread_{$threadid}_{$pid}.htm?$salt#reply$floor')"));
}

function _delete(){
	extract(user::init());
	if(!$ismanage) kc_tip('您无权删除页面');
	$id=kc_post('id',2,1);

	$db=new db;
	$db->delete('%s_page','id='.$id);
	$referer=kc_post('HTTP_REFERER');
	kc_ajax(array('JS'=>"go('$referer')"));
}


function kid($i='',$b=''){
	
	if(!empty($i) && $b=='1'){
		$db=new db;
		$image=$db->getRows_one('posts','kpath','pid='.$i);
		$s="<p><img src=\"".kc_iamg_taobao($image[kpath])."_350x350.jpg\"></p>";
		$kuisz=$db->select('relationships,metas','name,slug','',"relationships.mid=metas.mid and metas.nshow=1 and metas.type = 'tag' and relationships.pid=".$i);
		if(!empty($kuisz)){
		$s.='<p>相关标签：';
		foreach ($kuisz as $tag) {
			$s.='<a href="/tag/'.$tag[slug].'">'.$tag[name].'</a> ';
		}
		
		$s.='</p>';
		}
		return $s;
	}
	
	
}







?>